import datetime
from typing import List, Dict

from sqlalchemy import Column, DateTime, Date, JSON, String, TEXT, Index, func, and_, BIGINT
from sqlalchemy.orm import Session

from common.database import Base
from common.database import SessionMixin
from core.logger import logger


class ImpalaQueryComputeStat(Base, SessionMixin):
    __tablename__ = 'bigdata_impala_query_compute_stat'
    start_time = Column(DateTime, nullable=False, comment="开始时间")
    query_id = Column(String(128), nullable=False, comment="查询ID")
    statement = Column(TEXT, nullable=True, comment="查询语句")
    duration_millis = Column(BIGINT, nullable=True, comment="执行时长")
    produced_table_query_id = Column(String(128), nullable=True, comment="创建表查询ID")
    produced_table_statement = Column(String(128), nullable=True, comment="创建表语句")
    rows_inserted = Column(BIGINT, nullable=True, comment="插入行数")
    hdfs_bytes_written = Column(BIGINT, nullable=True, comment="写入hdfs字节")
    create_date = Column(Date, nullable=False, comment="创建日期")
    create_time = Column(DateTime, default=datetime.datetime.now, nullable=False, comment="创建时间")

    __table_args__ = (
        Index('idx_bigdata_impala_query_compute_stat_uuid', 'uuid', unique=True),
        Index('idx_bigdata_impala_query_compute_stat_query_id', 'query_id'),
        Index('idx_bigdata_impala_query_compute_stat_create_date', 'create_date'),
        Index('idx_bigdata_impala_query_compute_stat_create_time', 'create_time'),
        Index('idx_bigdata_impala_query_compute_stat_start_time', 'start_time'),
        {
            'mysql_engine': 'innodb',
            'mysql_charset': 'utf8mb4',
            'mysql_collate': 'utf8mb4_unicode_ci',
            'extend_existing': True,
            'mysql_row_format': 'dynamic',
        }
    )

    def __repr__(self):
        return "<query_id='{} ,statement='{} '>".format(self.query_id, self.statement)
